//
//  ChargeIndexView.m
//  ELife
//
//  Created by  刘奇男 on 11-5-13.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "ChargeIndexViewController.h"
#import "UnionPayIndexViewController.h"
#import "Constants.h"

@implementation ChargeIndexViewController

@synthesize chargeMethodList;
@synthesize chargeMethodKey;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [chargeMethodKey release];
    [chargeMethodList release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
	self.chargeMethodList = [NSMutableArray array];
    
    if (!chargeMethodKey)
	{
		chargeMethodKey = [[NSArray alloc] initWithObjects:@"Charge1", @"Charge2", @"Charge3", @"Charge4", @"Charge5",  @"Charge6",  @"Charge7", nil];
    }
	
    for (NSString *methodKey in chargeMethodKey)
	{
        NSString* keyTitle = [methodKey stringByAppendingString:@"Title"];
        NSString* keyExplain = [methodKey stringByAppendingString:@"Explain"];
        NSString* keyClassName = [methodKey stringByAppendingString:@"ClassName"];        
        
        NSString* valueTitle =  NSLocalizedString(keyTitle, @"");
        NSString* valueExplain = NSLocalizedString(keyExplain, @"");
        NSString* valueClassName = NSLocalizedString(keyClassName, @"");
        
        NSMutableDictionary* dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   valueTitle, kTitleKey, 
                                   valueExplain, kDetailKey,
                                   valueClassName, kClassNameKey,
                                   nil];
        
		[self.chargeMethodList addObject: dic];
	}
    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
    [self.tableView setBackgroundView:backImg];
    
    [backImg release];
    
    [self.navigationItem setTitle:@"账户充值"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
	[self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:NO];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    // Return the number of rows in the section.
    return chargeMethodKey.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *kCellIdentifier = @"ChargeCellID";
    
    
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellIdentifier] autorelease];
    }
    
    // Configure the cell...'
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.textLabel.opaque = NO;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.highlightedTextColor = [UIColor whiteColor];
    cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
    
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.opaque = NO;
    cell.detailTextLabel.textColor = [UIColor grayColor];
    cell.detailTextLabel.highlightedTextColor = [UIColor whiteColor];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];

    
    NSDictionary *dataDictionary = [chargeMethodList objectAtIndex:indexPath.row];
    cell.textLabel.text = [dataDictionary valueForKey:kTitleKey];
    cell.detailTextLabel.text = [dataDictionary valueForKey:kDetailKey];
    
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.chargeMethodList.count - 2)
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.sina.com.cn"]];
        return;
    }
    
    NSMutableDictionary *rowData = [self.chargeMethodList objectAtIndex:indexPath.row];
	UIViewController *targetViewController = [rowData objectForKey:kViewControllerKey];
	if (!targetViewController)
	{
        NSString *viewName = [rowData objectForKey:kClassNameKey];
        
        targetViewController = [[NSClassFromString([viewName stringByAppendingString:@"ViewController"]) alloc] initWithNibName:[viewName  stringByAppendingString:@"View"] bundle:nil];
        
        [rowData setValue:targetViewController forKey:kViewControllerKey];
        
        [targetViewController release];
    }
    [self.navigationController pushViewController:targetViewController animated:YES];
}

@end
